package com.javatest;

public class Ticketing implements Runnable {
    private int count=10;  //记录剩余票数
    private int num = 0;   //记录买到第几张票

    private  boolean flag=true;
    @Override
    public void run() {
       /* while (flag){
            sale();
        }*/
        while (true){
           synchronized (this){
               if(count<=0){
                   break;
               }
               count--;
               num++;
               try {
                   Thread.sleep(500);
               }catch (Exception e){
                   e.printStackTrace();
               }
               System.out.println(Thread.currentThread().getName()+"抢到第"+num+"张票，剩余"+count+"张");
           }
        }
    }



    public synchronized void sale(){
        if(count<=0){
            flag=false;
            return;
        }
        count--;
        num++;
        try {
            Thread.sleep(500);
        }catch (Exception e){
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+"抢到第"+num+"张票，剩余"+count+"张");

    }
}
